package com.infinitus.nginxInfo.domain.switchinfo;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSONObject;
import com.infinitus.nginxInfo.config.StaticCache;
import com.infinitus.nginxInfo.domain.nginxinfo.Fields;
import com.infinitus.nginxInfo.domain.nginxinfo.Geoip;
import com.infinitus.nginxInfo.service.util.Base64Coder;
import com.infinitus.nginxInfo.service.util.TimeUtil;
import com.infinitus.nginxInfo.task.FilterRules;

public class SwitchInfo  implements Serializable{

	public static final String ITEM_TERMINATED = "\001";

	public static final String COLLECTION_TERMINATED = "\002";
	
	public static final String HEAD_TERMINATED = "\\|";

	public static final String MAP_TERMINATED = "\003";
	/**
	 * 
	 */
	private static final long serialVersionUID = -4667668210163533600L;
	private String msg;
	private String level;
	private String ip;
	private String message;
	private Fields fields;
	private Geoip geoip;
	
	private String event_time;
	private String YearMonth;
	private String YearWeek;
	private String DayOfWeek;
	private String EventDate;
	private String EventHour;
	
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getLevel() {
		return level;
	}
	public void setLevel(String level) {
		this.level = level;
	}
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Fields getFields() {
		return fields;
	}
	public void setFields(Fields fields) {
		this.fields = fields;
	}
	public Geoip getGeoip() {
		return geoip;
	}
	public void setGeoip(Geoip geoip) {
		this.geoip = geoip;
	}
	public String getEvent_time() {
		return event_time;
	}
	public void setEvent_time(String event_time) {
		this.event_time = event_time;
	}
	public String getYearMonth() {
		return YearMonth;
	}
	public void setYearMonth(String yearMonth) {
		YearMonth = yearMonth;
	}
	public String getYearWeek() {
		return YearWeek;
	}
	public void setYearWeek(String yearWeek) {
		YearWeek = yearWeek;
	}
	public String getDayOfWeek() {
		return DayOfWeek;
	}
	public void setDayOfWeek(String dayOfWeek) {
		DayOfWeek = dayOfWeek;
	}
	public String getEventDate() {
		return EventDate;
	}
	public void setEventDate(String eventDate) {
		EventDate = eventDate;
	}
	public String getEventHour() {
		return EventHour;
	}
	public void setEventHour(String eventHour) {
		EventHour = eventHour;
	}
	public void setEvent_time(Long time) {
		Date date = new Date();
		try{
			Date eventTime = new Date(time);
			if(eventTime.before(date)){
				date = eventTime;
			}
		}catch(Exception e){
			
		}
		this.event_time = ""+date.getTime();
		String dateStr = TimeUtil.dateTimeFormat.format(date);
		YearMonth = dateStr.substring(0, 7);
		EventDate = dateStr.substring(0, 10);
		EventHour = dateStr.substring(11, 13);
		DayOfWeek = date.toString().substring(0, 3);
	}
	
	public static<T> T converts(String value, Class<T> clazz) {
		T item = null;
		try{
			JSONObject json = JSONObject.parseObject(value);
			item = JSONObject.toJavaObject(json, clazz);
			String time = json.getString("@timestamp");
			Method method = clazz.getDeclaredMethod("setEvent_time", Long.class);
			method.invoke(item, TimeUtil.isoToUnix(time));
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("解析错误："+value);
		}
		return item;
	}
	
	public static SwitchInfo converts(String value) {
		SwitchInfo item = null;
		try{
			JSONObject json = JSONObject.parseObject(value);
			item = JSONObject.toJavaObject(json, SwitchInfo.class);
			String time = json.getString("@timestamp");
			item.setEvent_time(TimeUtil.isoToUnix(time));
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("解析错误："+value);
		}
		return item;
	}
	
	
	public static final Map<String,String> FILEDTYPE_MAP;
	static{
		FILEDTYPE_MAP = new HashMap<String, String>();
		FILEDTYPE_MAP.put("business_name","s");
		FILEDTYPE_MAP.put("app_name","s");
		FILEDTYPE_MAP.put("app_version","s");
		FILEDTYPE_MAP.put("file_type_name","s");
		FILEDTYPE_MAP.put("machine_name","s");
		
		FILEDTYPE_MAP.put("sugo_nation","s");
		FILEDTYPE_MAP.put("sugo_province","s");
		FILEDTYPE_MAP.put("sugo_city","s");
		FILEDTYPE_MAP.put("sugo_latitude","f");
		FILEDTYPE_MAP.put("sugo_longitude","f");
		
		FILEDTYPE_MAP.put("msg","s");
		FILEDTYPE_MAP.put("level","s");
		FILEDTYPE_MAP.put("sugo_ip","s");
		FILEDTYPE_MAP.put("message","s");
		FILEDTYPE_MAP.put("event_time","d");
		FILEDTYPE_MAP.put("YearMonth","s");
		FILEDTYPE_MAP.put("YearWeek","s");
		FILEDTYPE_MAP.put("DayOfWeek","s");
		FILEDTYPE_MAP.put("EventDate","s");
		FILEDTYPE_MAP.put("EventHour","s");
	}
	
	public Map<String ,Object> toMap(){
		Map<String, Object> map = new HashMap<String, Object>();
		if(fields!=null){
			cutField(map, "business_name", fields.getBusiness_name());
			cutField(map, "app_name", fields.getApplication_name());
			cutField(map, "app_version", fields.getEnvironment_type());
			cutField(map, "file_type_name", fields.getFile_type_name());
			cutField(map, "machine_name", fields.getMachine_name());
		}
		if(geoip!=null){
			cutField(map, "sugo_nation", geoip.getCountry_name());
			cutField(map, "sugo_province", geoip.getRegion_name());
			cutField(map, "sugo_city", geoip.getCity_name());
			cutField(map, "sugo_latitude", geoip.getLatitude());
			cutField(map, "sugo_longitude", geoip.getLongitude());
		}
		cutField(map, "msg", msg);
		cutField(map, "level", level);
		cutField(map, "sugo_ip", ip);
		cutField(map, "message", message);
		
		cutField(map, "event_time", event_time);
		cutField(map, "YearMonth", YearMonth);
		cutField(map, "YearWeek", YearWeek);
		cutField(map, "DayOfWeek", DayOfWeek);
		cutField(map, "EventDate", EventDate);
		cutField(map, "EventHour", EventHour);
		return map;
	}
	
	private void cutField(Map<String, Object> map ,String FieldName,String FieldValue){
		if(FilterRules.isNotBlank(FieldValue)){
			map.put(FieldName, FieldValue );
		}
	}
	
	
	public static String encrypt(String targetTopic, SwitchInfo item) {
		Map<String, Object> map = item.toMap();
		StringBuffer headStr = new StringBuffer();
		StringBuffer bodyStr = new StringBuffer();
		for(Entry<String, Object> entry: map.entrySet()){
			String field = entry.getKey();
			String type = StaticCache.getFiledType(targetTopic+field);
			if(type == null){
				type=SwitchInfo.FILEDTYPE_MAP.get(field);
			}
			if(type==null) continue;
			headStr.append(type+"|"+field+",");
			bodyStr.append(entry.getValue()+ SwitchInfo.ITEM_TERMINATED);
		}
		if(headStr.length()==0){
			return null;
		}
		String head = headStr.substring(0, headStr.length()-1);
		String body = bodyStr.substring(0, bodyStr.length()-1);
//		System.out.println("日期："+TimeUtil.getCurrentTime()+" 发送topic："+targetTopic +"_etl\r\n发送头："+head+"\r\n内容体："+body);
		return item.getEvent_time()+"\t"+item.getIp()+"\t2\t3\t"+item.getIp()+"\t5\t6\t" +Base64Coder.encodeString(head +SwitchInfo.COLLECTION_TERMINATED+body);
	}
}
